package cn.sanch.pet.controller;

import cn.sanch.basic.loginContext.LoginContext;
import cn.sanch.basic.util.BusinessException;
import cn.sanch.pet.dto.MsgDto;
import cn.sanch.pet.service.ISearchMasterMsgService;
import cn.sanch.pet.domain.SearchMasterMsg;
import cn.sanch.pet.query.SearchMasterMsgQuery;
import cn.sanch.basic.util.AjaxResult;
import cn.sanch.basic.util.PageList;
import cn.sanch.user.domain.Logininfo;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null) {
                searchMasterMsgService.update(searchMasterMsg);
            }else {
                searchMasterMsgService.save(searchMasterMsg);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
                searchMasterMsgService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public SearchMasterMsg queryOne(@PathVariable("id")Long id) {
        return searchMasterMsgService.findOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<SearchMasterMsg> queryAll(){
        return searchMasterMsgService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }


    /**
     * 接口：发布寻主消息
     * @param searchMasterMsg
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            searchMasterMsgService.publish(searchMasterMsg, request);
            return AjaxResult.me();
        } catch (BusinessException b) {
            b.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(b.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发布失败！");
        }
    }

    /** 【后台页面】
     * 接口：查询待审核或者驳回状态的寻主信息对象
     * - 平台管理员才能查看 - state=0 - 没有shopId
     * @param query
     * @return
     */
    @PostMapping("toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query) {
        // 设置state状态为0
        query.setState(0);
        return searchMasterMsgService.queryPage(query);
    }


    /** 【后台页面】
     * 接口：查询寻主池的寻主信息对象
     * - 审核通过state=1，但是没有shopId shopId is null
     * @param query
     * @return
     */
//    @PostMapping("searchMasterMsgPool")
//    public PageList<SearchMasterMsg> searchMasterMsgPool(@RequestBody SearchMasterMsgQuery query) {
//        // 设置state状态为1
//        query.setState(1);
//        return searchMasterMsgService.searchMasterMsgPool(query);
//    }

    /** 【后台页面】
     * 接口：查询已完成的寻主信息对象
     * - state=2
     * @param query
     * @return
     */
    @PostMapping("over")
    public PageList<SearchMasterMsg> over(@RequestBody SearchMasterMsgQuery query) {
        // 设置state状态为0
        query.setState(2);
        return searchMasterMsgService.queryPage(query);
    }

    /** 【前台页面】
     * 接口：用户自己看自己发布的寻主信息
     * 有userId
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return searchMasterMsgService.user(query, request);
    }

    /** 【后台页面】
     * 接口：查看待处理的寻主信息
     *
     * 平台管理员 - 能看到所有的待处理寻主信息 - state=1
     * shopId is not null因为要看到所有的待处理，所有shopId不为null但是又不知道具体shopId
     *
     * 店铺管理员 - 只能看到直接店铺待处理的寻主信息 - state=1 ， 有具体shopId，就是本店铺的id
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return searchMasterMsgService.toHandle(query, request);
    }


    /**
     * 接口：拒绝订单 - 将shopId置空
     * @param msgId
     * @return
     */
    @GetMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId) {
        try {
            searchMasterMsgService.reject(msgId);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("拒绝失败！");
        }
    }

    /**
     * 接口：接单
     * @param msgDto
     * @return
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody MsgDto msgDto){
        try {
            searchMasterMsgService.accept(msgDto);
            return AjaxResult.me();
        } catch (BusinessException b) {
            b.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(b.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("接单失败！");
        }
    }


}
